const server = require("./server/server.js");
//app.js
App({
    onLaunch:function(){
        wx.cloud.init({
            taceUser:true,
            env:"test-usd92"
        });
        this.getWXscope();
    },
    getWXscope() {
        wx.getSetting({
            success: ({authSetting})=>{
                console.log(authSetting)           
                if(authSetting["scope.userInfo"]){
                    this.getUserInfo();
                }else{
                    this.showModal();
                }
            },
        });
    },
    getUserInfo() {
        wx.getUserInfo({
            withCredentials: false,
            lang: 'zh_CN',
            success: (result)=>{
                // console.log(result);
                this.userInfo = result.userInfo;
                wx.cloud.callFunction({
                    name: "getUserOpenId",
                    success:(data) => {
                        console.log(data.result.openid);
                        this.userInfo.openId = data.result.openid;
                        server.findUserByOpenId(data.result.openid)
                        .then(({ result })=>{
                            // console.log();
                            if(result) {
                                // console.log(result);
                                this.userInfo.id = result.id;
                                // console.log(user);
                            }else{
                                this.showModal();
                            }
                        })
                        .catch(server.errorFunction)
                    }
                })
            },
        });
    },
    showModal() {
        wx.showModal({
            title:'授权注册',
            content:'需要完成授权注册后，才可以使用完整功能',
            showCancel:true,
            cancelText:'不注册',
            cancelColor:"#000000",
            confirmText:'去注册',
            confirmColor:"#3cc51f",
            success:(result) => {
                if(result.confirm){
                    wx.switchTab({
                        url: '/pages/user/user'
                    })
                }
            }
        })
    },
    userInfo: null
})